package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.request.DistributeRequest;
import com.primary.entity.Distribute;
import com.primary.service.DistributeService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/distribute")
public class DistributeController {
    final
    DistributeService distributeService;

    public DistributeController(DistributeService distributeService) {
        this.distributeService = distributeService;
    }

    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(DistributeRequest distributeRequest){
        return Result.success(distributeService.queryByPage(distributeRequest));
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Distribute distribute) {
        if (distribute.getBloodType() != null) {
            return distributeService.insertOne(distribute) > 0 ?
                    Result.success() : Result.error("-114","插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody Distribute distribute){
        if (null != distribute.getBloodType()){
            return distributeService.updateOneById(distribute) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
        return null == id ? Result.error("ID不能为空") :
                (distributeService.deleteById(id) ? Result.success():Result.error("删除失败"));
    }
}
